home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 039a / bplus25a.zip / REINDEX.C < prev    next >
Text File  |  1990-12-30  |  4KB  |  101 lines

  1. /********************************************************************/
  2. /*                            REINDEX.C                             */
  3. /*                                                                  */
  4. /* This is a sample program which demonstrates how to reindex a     */
  5. /* data file.  The data file which is used is the one created by    */
  6. /* NAMES.C which creates an online address book using the B-PLUS    */
  7. /* file indexing toolkit.                                           */
  8. /*                                                                  */
  9. /********************************************************************/
  10.  
  11.  
  12. #include <stdio.h>
  13. #include <process.h>
  14. #include <string.h>
  15. #include "bplus.h"
  16.  
  17.  
  18. typedef struct              /* Here is the address record definition */
  19.   {
  20.      char lastname[16];     /* last name           */
  21.      char firstname[16];    /* first name          */
  22.      char address1[31];     /* first address line  */
  23.      char address2[31];     /* second address line */
  24.      char city[21];         /* the city            */
  25.      char state[3];         /* the state           */
  26.      char zipcode[6];       /* postal zip code     */
  27.      char telephone[14];    /* telephone number    */
  28.   }  ADDRESS;
  29.  
  30.  
  31. IX_DESC  nameindex;             /* index file variable  */
  32. FILE     *namefile;             /* data file pointer    */
  33. ADDRESS  person;                /* data record variable */
  34.  
  35.  
  36. void openfiles(void);
  37. void closefiles(void);
  38. long makeindex(void);
  39.  
  40.  
  41. void openfiles()
  42.   /* The file NAMES.DAT already exists.  It is  opened and a new  */
  43.   /* index file is created.                                       */
  44.   {
  45.     if ((namefile = fopen("names.dat","r")) != NULL)
  46.       make_index("names.idx", &nameindex, 1);   /* create index file */
  47.     else
  48.     {
  49.       printf("\nUnable to open NAMES.DAT\n");
  50.       exit(1);
  51.     }
  52.   } /* openfiles */
  53.  
  54.  
  55. void closefiles()
  56.   /* close all files and exit */
  57.   {
  58.     fclose(namefile);
  59.     close_index(&nameindex);
  60.   } /* closefiles */
  61.  
  62.  
  63. long makeindex()
  64.   /* read address records and add key to index file */
  65.   {
  66.     ENTRY ee;
  67.     long num, position;
  68.     int  ret, size;
  69.     size = sizeof(person);                    /* data record size */
  70.     ret = (int)fread(&person,size,1,namefile);  /* ret is number of data  */
  71.                                               /* records that were read */
  72.     num = 0L;                                 /* num = data items read */
  73.     position = 0L;                            /* position in datafile */
  74.     while (ret == 1)
  75.       {
  76.         strcpy(ee.key, person.lastname);      /* key is last name followed */
  77.         strcat(ee.key, person.firstname);     /* first name.  Capitalize   */
  78.         strupr(ee.key);                  /*    and copy to ee.key.    */
  79.         ee.recptr = position;                 /* position in datafile  */
  80.         if (add_key(&ee, &nameindex) != IX_OK)     /* add key to index */
  81.            printf("Error while adding key to index file");
  82.         position += size;                     /* new position in datafile */
  83.         num++;                                /* increment data items read */
  84.         ret = fread(&person,sizeof(person),1,namefile);
  85.       }
  86.       return ( num );
  87.   } /* makeindex */
  88.  
  89.  
  90. void main()
  91.   /* Here is the main program */
  92.   {
  93.     long num;
  94.     openfiles();
  95.     printf("\n\n     MAKING THE NEW INDEX FILE\n\n");
  96.     num = makeindex();
  97.     closefiles();
  98.     printf("     REINDEXING IS COMPLETE - %ld ITEMS WERE INDEXED\n", num);
  99.   } /* main */
  100.  
  101.